package com.soccertgc.logging
{
	import com.soccertgc.comm.Call;
	import com.soccertgc.comm.CallErrorEvent;
	import com.soccertgc.comm.CallEvent;
	import com.soccertgc.comm.CallFactory;
	import com.soccertgc.comm.CallFailEvent;
	import com.soccertgc.comm.CallSuccessEvent;
	
	public class RemoteLogTarget extends LogTarget
	{
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _call:Call;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function RemoteLogTarget(options:LogTargetParams)
		{
			// Ancestor
			super(LogTargetType.REMOTE, options);
			// Initialization
			_call = new Call(CallFactory.CALL_LOG, true);
			_call.addEventListener(CallEvent.EVENT_CALL_SUCCESS, 	logCallSuccessHandler);
			_call.addEventListener(CallEvent.EVENT_CALL_FAIL, 		logCallFailHandler);
			_call.addEventListener(CallEvent.EVENT_CALL_ERROR, 		logCallErrorHandler);
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		protected function logCallSuccessHandler(e:CallSuccessEvent):void
		{
			// do nothing
		}
		
		protected function logCallFailHandler(e:CallFailEvent):void
		{
			trace("");
			trace("################################################################");
			trace("## RemoteLogTarget #############################################");
			trace("Remote call failed: " + e.toString());
			trace("################################################################");
			trace("");
		}
		
		protected function logCallErrorHandler(e:CallErrorEvent):void
		{
			trace("");
			trace("################################################################");
			trace("## RemoteLogTarget #############################################");
			trace("RemoteLogTarget call error" +  e.toString());
			trace("################################################################");
			trace("");
		}		
		
		override protected function write(message:String):void
		{ 
			// Messages
			var mgs:Array = [];
			// Check if the message contains the internal handling for multilines
			if (isMultiline(message))
			{
				// Splits the lines
				mgs = split(message);
			}
			else
			{
				// Trace the single message
				mgs = [ message ];
			}
			// Clear the call
			_call.clear();
			// Call parameters
			_call.params = new Object();
			_call.params["logMsg[]"] = mgs;
			// Executes the server call for log
			_call.execute();
		}
		
	}
}